;
(function (call) {
    var main = {
        userInfo: null,
        mapId: "jcybMap",
        // skTime: '2020-07-06 08:00:00',
        // skTime: '2021-08-25 08:00:00',
        skTime:'',
        elementCfg: {
            TP10M: '10分钟降水', TP1H: '1小时降水', TP3H: '3小时降水', TP6H: '6小时降水', TP12H: '12小时降水', GUST: '阵风', GALE: '10m风', VIS: '能见度',
            TMP: '高温', LIGHT: '闪电', HAIL: '冰雹', RADAR: '回波报警', JL: '积涝', GEO: '地质灾害', MFRInfo: '山洪', ZXHL: '中小河流', SLHX: '森林火险', YJXH: '预警信号',
            SMG: '雷暴大风', RAT: '短时强降水', MDBZ: '组合反射率'
        },
        alarmCfg: {
            'sk': { AWS: '自动站', LIGHT: '闪电定位仪', RADAR: '雷达', JSZ: '积水站', TQSB: '强天气识别', ZQZB: '灾情直报' },
            'yb': { JJSJ: '卷积神经外推', TREC: 'TREC外推', GDMODE: '主观格点', NWGD: '国家指导', EC: 'EC', TJWRF: 'TJWRF', YJXH: '预警信号', 'C-2DCNN': 'C-2DCNN' }
        },
        warnType: {
            'S_RAIN10M': { element: 'TP10M', type: 'AWS' },
            'S_RAIN1': { element: 'TP1H', type: 'AWS' },
            'S_RAIN3': { element: 'TP3H', type: 'AWS' },
            'S_GALE': { element: 'GALE', type: 'AWS' },
            'S_GUST': { element: 'GUST', type: 'AWS' },
            'S_V': { element: 'VIS', type: 'AWS' },
            'S_H_T': { element: 'TMP', type: 'AWS' },
            'S_TP12H': { element: 'TP12H', type: 'ZQZB' },
            'S_HAIL': { element: 'HAIL', type: 'ZQZB' },
            'S_GEO': { element: 'GEO', type: 'ZQZB' },
            'S_THUNDER': { element: 'LIGHT', type: 'ZQZB' },
            'S_LIGHT': { element: 'LIGHT', type: 'LIGHT' },
            'T_HAIL': { element: 'HAIL', type: 'TQSB' },
            'T_RAIN': { element: 'TP1H', type: 'TQSB' },
            'Z_TP1H-1': { element: 'TP1H', type: 'GDMODE' },
            'Z_TP1H-3': { element: 'TP3H', type: 'GDMODE' },
            'Z_TP1H-6': { element: 'TP6H', type: 'GDMODE' },
            'Z_TP1H-12': { element: 'TP12H', type: 'GDMODE' },
            'Z_EDA': { element: 'EDA', type: 'GDMODE' },
            'Z_VIS': { element: 'VIS', type: 'GDMODE' },
            'Z_TMP': { element: 'TMP', type: 'GDMODE' },
            'Z_YJXH': { element: 'YJXH', type: 'YJXH' },
            'Z_QHRADA_MCR': { element: 'RADAR', type: 'JJSJ' },
            'Z_NDRADA_COMREFFORCAST': { element: 'RADAR', type: 'TREC' },
            'Z_RAT': { element: 'RAT', type: 'C-2DCNN' },
            'Z_SMG': { element: 'SMG', type: 'C-2DCNN' },
            'Z_MDBZ': { element: 'MDBZ', type: 'TJWRF' },
        },
        options: {
            dlLen: 2,     // 短临的预报时间, 默认2小时
            dlInterval: 10, // 短临的预报间隔, 默认10分钟
            dqLen: 70,      // 短期的预报时间, 默认70小时
            dqInterval: 1,  // 短期的预报间隔, 默认1小时
        },
        init: function () {
            this.bindEvt();
            this.initView();
        },
        initView: function () {
            this.skTime = $("#datepicker .datepicker-date span").text() + ":00";
            var userInfo = $("#userInfo").data('info');
            this.userInfo = userInfo;
            this.initDetailsTop();
            this.initTableHead();
            this.initTimeline();
            this.queryWarnData();
        },
        queryWarnData: function () {
            this.queryWarnInfo();
            this.queryDisasterReport();
            this.queryQtqsb();
        },
        initTimeline: function (type) {
            var that = this;
            var elementData = that.elementCfg;
            var startTime = new Date(this.skTime);
            var $ul = $(".timeline").empty();
            var $sk_li = $(`<li class="sk">${startTime.format("hh:mm")}</li>`);
            $sk_li.data('time', startTime.format("yyyy-MM-dd hh:mm:ss"));
            $sk_li.attr('title', startTime.format("yyyy-MM-dd hh:mm"));
            $ul.append($sk_li);

            var lastDlTime, timeStr;
            var dlInterval = that.options.dlInterval * 60 * 1000;
            var dlCounts = that.options.dlLen * 60 / that.options.dlInterval;
            for (var i = 1; i <= dlCounts; i++) {
                var tempTime = new Date(startTime.getTime() + dlInterval * i);
                lastDlTime = tempTime;
                var $dl_li = $(`<li>${tempTime.format("hh:mm")}</li>`);
                $dl_li.data('time', tempTime.format("yyyy-MM-dd hh:mm:ss"));
                $dl_li.attr('title', tempTime.format("yyyy-MM-dd hh:mm"));
                $dl_li.addClass('yb');
                $ul.append($dl_li);
            }

            var dqInterval = that.options.dqInterval * 60 * 60 * 1000;
            var startDate = new Date(lastDlTime.getTime() + dqInterval);
            var clsNm = '';
            for (var i = 0; i < that.options.dqLen; i++) {
                var tempTime = new Date(startDate.getTime() + dqInterval * i);
                tempTime.setMinutes(0);
                if (tempTime.getHours() == 0) {
                    timeStr = tempTime.format("dd日");
                    clsNm = 'day'
                } else {
                    timeStr = tempTime.format("hh:mm");
                    clsNm = '';
                }
                var $dq_li = $(`<li class=${clsNm}>${timeStr}</li>`);
                $dq_li.data('time', tempTime.format("yyyy-MM-dd hh:mm:ss"));
                $dq_li.attr('title', tempTime.format("yyyy-MM-dd hh:mm:ss"));
                $dq_li.addClass('yb');
                $ul.append($dq_li);
            }
            $ul.css('width', $ul.find('li').length * 100);
            $('.details-table .table-body').css('width', $ul.find('li').length * 100);

            var length = $(".timeline li").length, $trs = [];
            for (var key in elementData) {
                var $tr = $('<tr>'), tds = [];
                for (i = 0; i < length; i++) {
                    tds.push(`<td></td>`);
                }
                $trs.push($tr.html(tds.join('')));
            }
            $('.details-table .table-body table').html($trs);
        },
        initDetailsTop: function () {
            var that = this;
            var $sk = $(".details-list.sk").empty();
            var $yb = $(".details-list.yb").empty();
            for (var key in that.alarmCfg['sk']) {
                var $li = $(`<li><i class=${key.toLocaleLowerCase()}></i>${that.alarmCfg['sk'][key]}</li>`);
                $li.data('type', key);
                $sk.append($li);
            }
            for (var key in that.alarmCfg['yb']) {
                var $li = $(`<li><i class=${key.toLocaleLowerCase()}></i>${that.alarmCfg['yb'][key]}</li>`);
                $li.data('type', key);
                $yb.append($li);
            }
        },
        // 详情表格
        initTableHead: function (type) {
            var that = this;
            var $head = $(".table-head table").empty();
            var elementData = that.elementCfg;
            for (var key in elementData) {
                var th = '<tr><th>' + elementData[key] + '</th></tr>'
                var $th = $(th).data('element', key);
                $head.append($th);
            }
        },
        queryWarnInfo: function (type) { // 查询实况，预报报警信息
            var that = this;
            var param = {
                time: this.skTime,
                areaCode: this.userInfo.userAreaCode
            }
            $.getJSON(api.internal.business.warnInfo, param, function (res) {
                var warnList = res.data || {};
                that.warnIconView(warnList,type);
            })
        },
        queryDisasterReport: function () { // 灾情直报
            var that = this;
            var enddate = that.skTime;
            var startdate = new Date(enddate).setHours(new Date(enddate).getHours() - 1);
            startdate = new Date(startdate).format("yyyy-MM-dd hh:mm:ss");
            var param = {
                "startdate": startdate,
                "enddate": enddate
            }
            $.post(api.external.disasterData, param, function (res) {
                data = res.data;
                if (!data || data.length === 0) return;

                data = $.grep(data, function (n, i) {
                    return Numbern.st == '1'; // 过滤状态为1的数据
                })
                var warnList = {};
                warnList[that.skTime] = { 'S_TP12H': [], 'S_HAIL': [], 'S_GEO': [], 'S_THUNDER': [] };
                data.forEach(function (d) {
                    if (d.zqtype == '11B03')
                        warnList[that.skTime]['S_TP12H'].push(data); // 暴雨
                    if (d.zqtype == '11B14')
                        warnList[that.skTime]['S_THUNDER'].push(data); // 雷电
                    if (d.zqtype == '11B15')
                        warnList[that.skTime]['S_HAIL'].push(data); // 冰雹
                })

                that.warnIconView(warnList);
            }, 'json')
        },
        queryQtqsb: function () { // 强天气识别
            var that = this;
            $.getJSON(api.internal.business.qtqsb, { time: that.skTime }, function (res) {
                if (!res.isSuccess || !res.data) return;
                var warnList = {};
                warnList[that.skTime] = {};
                for (var key in res.data) {
                    warnList[that.skTime][key] = res.data[key].features;
                }
                that.warnIconView(warnList);
            })
        },
        warnIconView: function (warnList,type) { // 报警图标展示
            var that = this;
            var elementData = that.elementCfg;
            $(".timeline li").each(function (i, item) {
                var time = $(item).data('time');
                var warnData = warnList[time];
                if (!warnData) return;
                for (var key in warnData) {
                    if (warnData[key].length === 0 || !that.warnType[key]) continue;
                    var identify = [];
                    warnData[key].forEach(function (data) {
                        var j = 0;
                        var startPeriod = data.startPeriod;
                        var endPeriod = data.endPeriod;
                        var warnPath = encodeURI(data.warnPath);
                        var sign = [startPeriod, endPeriod, warnPath].join(',');
                        if (identify.includes(sign)) return;
                        identify.push(sign);
                        for (var element in elementData) {
                            if (that.warnType[key].element == element) {
                                var $td = $('.table-body tr').eq(j).find('td').eq(i);
                                var imgName = data.modelType ? data.modelType.toLocaleLowerCase() : that.warnType[key].type;
                                imgName = imgName.toLocaleLowerCase();
                                var warnLevel = data.warnLevel;
                                if (warnLevel)
                                    imgName = imgName + '-' + Number(warnLevel);
                                $td.append(`<img src="/business/jcyb/alarmDetails/css/images/${imgName}.png" class="${(i === 0 ? 'SK' : 'YB')} ${data.modelType && data.modelType != 'MDBZ' ? data.modelType : that.warnType[key].type}">`);
                            }
                            j++;
                        }
                    })
                }
            })
        },
        warnControl: function (type, isShow) { // 报警图标控制显隐
            if (isShow)
                $('.table-body tr td img.' + type).show();
            else
                $('.table-body tr td img.' + type).hide();
        },
        bindEvt: function () {
            var that = this;
            $("#modal_alarmDetails .close").click(function () {
                $("#modal_alarmDetails").remove();
                $("#alarmDetails").removeClass("active");
            });
            $(".details-top .sk-tab").click(function () {
                $(this).toggleClass('active');
                if ($(this).hasClass('active')) {
                    $(".details-list.sk li").removeClass('active');
                }
                that.warnControl('SK', $(this).hasClass('active'));
            })
            $(".details-top .yb-tab").click(function () {
                $(this).toggleClass('active');
                if ($(this).hasClass('active')) {
                    $(".details-list.yb li").removeClass('active');
                }
                that.warnControl('YB', $(this).hasClass('active'));
            })
            $(".details-list.sk").on('click', 'li', function () {
                $(this).toggleClass('active');
                if ($(this).hasClass('active') && $(".sk-tab").hasClass('active')) {
                    $(".sk-tab").removeClass('active');
                    that.warnControl('SK', false);
                }
                that.warnControl($(this).data('type'), $(this).hasClass('active'));
            })
            $(".details-list.yb").on('click', 'li', function () {
                $(this).toggleClass('active');
                if ($(this).hasClass('active') && $(".yb-tab").hasClass('active')) {
                    $(".yb-tab").removeClass('active');
                    that.warnControl('YB', false);
                }
                that.warnControl($(this).data('type'), $(this).hasClass('active'));
            })
        }
    };
    if (typeof call === 'function') {
        call(main);
        CafeJS.alarmDetails = main
    }
})(function (main) {
    CafeJS.models(['datepicker'], function () {
        main.init();
    });
});